package com.eco.fanliapp.job.goodsjob;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.AppGoodsMapper;
import com.eco.fanliapp.dao.SyUserCollectionMapper;
import com.eco.fanliapp.entity.AppGoods;
import com.eco.fanliapp.entity.SyLabelGoods;
import com.eco.fanliapp.entity.SyLabelType;
import com.eco.fanliapp.entity.SyUserCollection;
import com.eco.fanliapp.service.*;
import com.eco.fanliapp.vo.respone.ResponeResults;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * 推荐商品加入缓存
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class recommendGoodsLabelTask implements Job, Serializable {


    private static final Logger log = LoggerFactory.getLogger(recommendGoodsLabelTask.class);

    @Autowired
    private IUserCashschemeService userCashschemeService;
    @Autowired
    private IAppGoodsService appGoodsService;
    @Autowired
    private ITaoBaoService iTaoBaoService;

    @Autowired
    private SyUserCollectionMapper syUserCollectionMapper;

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    private AppGoodsMapper appGoodsMapper;
    @Autowired
    private ISyLabelGoodsService syLabelGoodsService;
    @Autowired
    private ISyLabelTypeService syLabelTypeService;

    @Autowired
    private ISyUserCollectionService syUserCollectionService;

    public recommendGoodsLabelTask() {
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();

        JobDataMap dataMap = jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long startTime = System.currentTimeMillis();
        log.info("***************************************推荐商品加入缓存 CollectionTask：executeCount=" + executeCount + "--startTime=" + startTime);

        try {
            String goods_label_key = "app_recommend_goods_label";

            QueryWrapper<AppGoods> queryWrapper = new QueryWrapper();
            queryWrapper.eq("recommend", 1);
            queryWrapper.in("item_top", 1, 7);
            List<AppGoods> recommendGoods = appGoodsMapper.selectList(queryWrapper);

            List<String> itemids = new ArrayList<>();
            for (AppGoods appGoods : recommendGoods) {
                itemids.add(appGoods.getItemid());
            }
            List<SyLabelGoods> labelNameList = new ArrayList<>();
            if (itemids.size() > 0) {
                QueryWrapper<SyLabelGoods> labelQueryWrapper = new QueryWrapper();
                labelQueryWrapper.eq("enable", 1);
                labelQueryWrapper.eq("deleted", 0);
                labelQueryWrapper.in("item_id", itemids);
                labelNameList = syLabelGoodsService.list(labelQueryWrapper);
            }

            QueryWrapper<SyLabelType> labelTypeQueryWrapper = new QueryWrapper();
            labelTypeQueryWrapper.eq("enable", 1);
            labelTypeQueryWrapper.eq("deleted", 0);
            List<SyLabelType> labelList = syLabelTypeService.list(labelTypeQueryWrapper);


            for (AppGoods appGoods : recommendGoods) {

                List LabelNameList = new ArrayList<>();
                if (labelNameList.size() > 0) {
                    for (SyLabelGoods syLabelGoods : labelNameList) {
                        if (syLabelGoods.getItemId().equals(appGoods.getItemid())) {
                            if (labelList.size() > 0) {
                                for (SyLabelType syLabelType : labelList) {
                                    if (syLabelGoods.getLabelCode().equals(syLabelType.getLabelCode())) {
                                        LabelNameList.add(syLabelType.getLabelName());
                                    }
                                }
                            }
                        }
                    }
                }
                if (redisTemplate.hasKey(goods_label_key)) {
                    Map<String, List> itemMap = (Map) redisTemplate.opsForValue().get(goods_label_key);
                    itemMap.put(appGoods.getItemid(), LabelNameList);
                    redisTemplate.opsForValue().set(goods_label_key, itemMap);
                } else {
                    Map map = new HashMap<>();
                    map.put(appGoods.getItemid(), LabelNameList);
                    redisTemplate.opsForValue().set(goods_label_key, map);
                }

            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        log.info("***************************************推荐商品加入缓存 CollectionTask：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - startTime));

    }
}
